package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.adapter.WorkContentFragmentAdapter;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * @description:
 * @author: wyk
 * @time: 2021/9/17 11:17
 */
public class WorkFragment extends BaseFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private WorkContentFragmentAdapter adapter;
    private List<String> names = new ArrayList<>();
    //下划线偏移距离
    private int initMargin;
    //屏幕宽度
    private int tabLayoutWidth;
    //屏幕密度
    private float screenDensity;

    public static WorkFragment newInstance() {
        WorkFragment workFragment = new WorkFragment();
        return workFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initData();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work, container, false);
        tabLayout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.view_pager);
        //ButterKnife.bind(this, view);

        adapter = new WorkContentFragmentAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        // 更新适配器数据
        adapter.setList(names);
        //设置title下划线宽度
        //TabLayoutUtil.reflex(tabLayout);
        viewPager.setOffscreenPageLimit(adapter.getCount());
        setView();
        return view;
    }

    /**
     * 设置下划线位置 及选中标题放大加粗效果
     */
    public void setView(){

        //第一次先设置下划线默认位置
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //如果有CustomView 先清除再添加 避免重复
                    TabLayout.Tab tabAt = tabLayout.getTabAt(tabLayout.getSelectedTabPosition());
                    View view = tabAt.getCustomView();
                    if (null != view){
                        tabAt.setCustomView(null);
                    }
                    tabAt.setCustomView(R.layout.custrom_tab);
                    TextView textView =tabAt.getCustomView().findViewById(R.id.t_tab);
                    //获取默认选中Tab

                    //获取标题
                    String title = tabAt.getText().toString().trim();
                    SpannableString spannableString = new SpannableString(title);
                    //设置字号
                    AbsoluteSizeSpan span = new AbsoluteSizeSpan(DisplayUtils.sp2px(getContext(), 17));
                    //设置颜色
                    ForegroundColorSpan colorSpan = new ForegroundColorSpan(getActivity().getResources().getColor(R.color.black));

                    StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);

                    spannableString.setSpan(span, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    spannableString.setSpan(colorSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    spannableString.setSpan(styleSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(spannableString);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //如果有CustomView 先清除再添加 避免重复
                View view = tab.getCustomView();
                if (null != view){
                    tab.setCustomView(null);
                }
                tab.setCustomView(R.layout.custrom_tab);
                TextView textView =tab.getCustomView().findViewById(R.id.t_tab);
                //获取标题
                String title = tab.getText().toString().trim();
                SpannableString spannableString = new SpannableString(title);
                //设置字号
                AbsoluteSizeSpan span = new AbsoluteSizeSpan(DisplayUtils.sp2px(getContext(), 17));
                //设置颜色
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(getActivity().getResources().getColor(R.color.black));

                StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);

                spannableString.setSpan(span, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                spannableString.setSpan(colorSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                spannableString.setSpan(styleSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                textView.setText(spannableString);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (null == tab || null == tab.getText()) {
                    return;
                }
                String title = tab.getText().toString().trim();
                SpannableString spannableString = new SpannableString(title);
                //设置颜色
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(getActivity().getResources().getColor(R.color.color_AAB2AE));
                //设置字号
                AbsoluteSizeSpan span = new AbsoluteSizeSpan(DisplayUtils.sp2px(getContext(), 15));
                spannableString.setSpan(span, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                spannableString.setSpan(colorSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                tab.setText(spannableString);
                tab.setCustomView(null);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initData() {
        names.add(getString(R.string.home_work_stu));
        names.add(getString(R.string.computer_exam));
        names.add(getString(R.string.ketangjianglian));
        names.add(getString(R.string.dk_work));
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

        }

    }


    @Override
    public void onDestroy() {
        //ALiYunManager.getInstance().unRegisterListener(aLiYunListener);
        super.onDestroy();
    }
}
